<!-- eslint-disable -->
<template>
  <el-card class="box-card">
    <el-form :inline="true" :model="searchForm" class="demo-form-inline">
      <el-form-item label="类型">
        <el-input v-model="searchForm.name" placeholder="课程类型"></el-input>
      </el-form-item>
      <el-form-item label="教练">
        <el-input v-model="searchForm.coach" placeholder="教练"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="search">查询</el-button>
      </el-form-item>
    </el-form>
    <el-button
      type="primary"
      icon="el-icon-plus"
      style="margin-bottom: 50px; margin-top: 50px"
      @click="dialogVisible = true"
      :disabled="role !== 'admin'"
      >添加课程</el-button
    >

    <el-table :data="classList" border style="width: 100%">
      <el-table-column
        prop="id"
        label="课程编号"
        width="80"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="name"
        label="课程名"
        width="100"
        align="center"
      ></el-table-column>
      <el-table-column
        align="center"
        prop="coach"
        label="教练姓名"
        width="100"
      ></el-table-column>
      <el-table-column
        prop="content"
        label="课程描述"
        width="500"
        align="center"
      ></el-table-column>

      <el-table-column
        prop="img"
        label="课程图片"
        width="150"
        class="imgwrap"
        align="center"
      >
        <template v-slot="{ row }">
          <img :src="row.img" alt />
        </template>
      </el-table-column>

      <el-table-column prop="address" label="操作" align="center">
        <template slot-scope="{ row, index }">
          <el-button
            type="danger"
            size="small"
            icon="el-icon-delete"
            title="删除课程"
            @click="deleteClass(row)"
            :disabled="role !== 'admin'"
          ></el-button>
          <el-button
            type="warning"
            size="small"
            icon="el-icon-edit"
            title="修改信息"
            @click="toUpdate(row)"
            :disabled="role !== 'admin'"
          ></el-button>
          <el-button
            type="info"
            size="small"
            icon="el-icon-truck"
            title="预约练车"
            @click="toPractice(row)"
            :disabled="role !== 'user'"
          ></el-button>
          <el-button
            type="success"
            size="small"
            icon="el-icon-s-order"
            title="预约考试"
            @click="toEaxm"
            :disabled="role !== 'user'"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :page-sizes="[4, 6, 8, 10]"
      :page-size="100"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    ></el-pagination>
    <!-- 添加的弹出框 -->
    <el-dialog title="添加课程" :visible.sync="dialogVisible" width="30%">
      <el-form :model="form">
        <el-form-item label="课程名">
          <el-input v-model="form.name" placeholder="请输入课程名"></el-input>
        </el-form-item>
        <el-select v-model="form.coach" placeholder="请选择">
          <el-option
            v-for="item in coachList"
            :key="item.id"
            :label="item.nickname"
            :value="item.nickname"
          >
          </el-option>
        </el-select>
        <el-form-item label="课程描述">
          <el-input v-model="form.content" placeholder="请输入姓名"></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addClass">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 修改弹出层 -->
    <el-dialog title="修改课程信息" :visible.sync="updateStatus" width="30%">
      <el-select v-model="updateForm.coach" placeholder="请选择">
        <el-option
          v-for="item in coachList"
          :key="item.id"
          :label="item.nickname"
          :value="item.nickname"
        >
        </el-option>
      </el-select>
      <span slot="footer" class="dialog-footer">
        <el-button @click="updateStatus = false">取 消</el-button>
        <el-button type="primary" @click="updateCoach">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 练车弹出层 -->
    <el-dialog title="确认练车信息" :visible.sync="practiceStatus" width="30%">
      <el-form :model="practiceForm">
        <el-form-item label="课程名">
          <el-input v-model="practiceForm.name" placeholder="请输入课程名"></el-input>
        </el-form-item>
        <el-form-item label="学员">
          <el-input v-model="practiceForm.nickname" placeholder="请输入课程名"></el-input>
        </el-form-item>
        <el-form-item label="教练">
          <el-input v-model="practiceForm.coach" placeholder="请输入课程名"></el-input>
        </el-form-item>
        <el-form-item label="地址">
          <el-input v-model="practiceForm.adress" placeholder="请输入课程名"></el-input>
        </el-form-item>
        <el-form-item label="图片">
          <el-input v-model="practiceForm.img" placeholder="请输入课程名"></el-input>
        </el-form-item>
        <el-form-item label="时间">
          <el-date-picker
            type="date"
            placeholder="选择日期"
            v-model="practiceForm.time"
          ></el-date-picker>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="practiceStatus = false">取 消</el-button>
        <el-button type="primary" @click="practice">确 定</el-button>
      </span>
    </el-dialog>
  </el-card>
</template>

<script>
/* eslint-disable */
import { classList, add, deleteC, updateCla, practiceCar,searchClass } from "@/api/class";
import { allcoach } from "@/api/coach";

import { mapState } from "vuex";

export default {
  name: "Class",
  data() {
    return {
      practiceStatus: false,
      updateStatus: false,
      coachList: [],
      dialogVisible: false,
      pageNumber: 1, //当前页
      pagesize: 4, //分页的数据
      total: 0, //总页数
      classList: [],
      form: {
        name: "",
        coach: "",
        content: "",
      },
      updateForm: {
        id: null,
        coach: "",
      },
      practiceForm: {
        name: "",
        nickname: "",
        coach: "",
        img: "",
        time: "",
        adress: "",
      },
      searchForm:{
        name:null,
        coach:null
      }
    };
  },
  methods: {
    handleSizeChange(val) {
      this.pagesize = val;
      this.getClassList();
    },
    handleCurrentChange(val) {
      this.pageNumber = val;
      this.getClassList();
    },
    async getClassList() {
      let res = await classList({
        pagesize: this.pagesize,
        pageNumber: this.pageNumber,
      });
      console.log(res);
      if (res.code == 200) {
        this.classList = res.data;
        this.total = res.totalCount;
      }
    },
    async getCoachList() {
      let res = await allcoach();
      if (res.code == 200) {
        this.coachList = res.data;
      }
    },
    async addClass() {
      let res = await add(this.form);
      console.log(res, "新增课程");
      if (res.code == 200) {
        this.$message({
          message: res.message,
          type: "success",
        });
        this.getClassList();
      }
      this.dialogVisible = false;
      this.form = {};
    },
    toUpdate(row) {
      this.updateForm.id = row.id;
      this.updateStatus = true;
    },
    // 删除课程
    async deleteClass(row) {
      let res = await deleteC(row.id);
      if (res.code == 200) {
        this.getClassList();
        this.$message({
          message: res.message,
          type: "success",
        });
      }
    },
    async updateCoach() {
      let res = await updateCla(this.updateForm);
      if (res.code == 200) {
        this.$message({
          message: res.message,
          type: "success",
        });
        this.getClassList();
      }
      this.updateStatus = false;
      this.updateForm = {};
    },
    toEaxm() {
      this.$router.push("/exams");
    },
    toPractice(row) {
      this.practiceStatus = true;
      this.practiceForm.name = row.name;
      this.practiceForm.nickname = this.info.nickname;
      this.practiceForm.coach = row.coach;
      this.practiceForm.img = row.img;
    },
    async practice() {
      let res = await practiceCar(this.practiceForm);
      if (res.code == 200) {
        this.$message({
          message: res.message,
          type: "success",
        });
      }
      this.practiceStatus = false;
      this.practiceForm = {};
    },
    async search() {
      if (this.searchForm.name == "") this.searchForm.name = null;
      if (this.searchForm.coach == "") this.searchForm.coach = null;
     
      if (this.searchForm.name == null && this.searchForm.coach==null){
        this.getClassList()
        return
      }
     
      let res = await searchClass(this.searchForm);
      if (res.code == 200) {
        this.$message({
          message: res.message,
          type: "success",
        });
        this.classList = res.data;
        this.total = res.data.length;
      }
     
     
    },
  },
  mounted() {
    this.getClassList();
    this.getCoachList();
  },
  computed: {
    ...mapState({
      role: (store) => store.user.info.role,
      info: (store) => store.user.info,
    }),
  },
};
</script>

<style scoped>
img {
  width: 100px;
  height: 100px;
  margin-right: 15px;
}
.box-card {
  width: 1200px;
  margin-left: 50px;
  margin-top: 50px;
}
</style>
